package com.nbwyctf.helper.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author chetaifu
 */
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext APPLICATION_CONTEXT;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        APPLICATION_CONTEXT = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return APPLICATION_CONTEXT;
    }

    /**
     * 根据bean名称获取某个bean对象
     */
    public static Object getBean(String name) throws BeansException {
        return APPLICATION_CONTEXT.getBean(name);
    }

    /**
     * 根据bean的class获取某个bean对象
     */
    public static <T> T getBean(Class<T> clazz) {
        return APPLICATION_CONTEXT.getBean(clazz);
    }

    /**
     * 获取spring.profiles.active
     */
    public static String getProfile() {
        return APPLICATION_CONTEXT.getEnvironment().getActiveProfiles()[0];
    }
}
